package com.ethen.jzvd;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ComponentParent;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.net.NetCapabilities;
import ohos.net.NetManager;

import java.util.Formatter;
import java.util.Locale;

/**
 * Created by Nathen
 * On 2016/02/21 12:25
 */
public class JZUtils {
    public static final String TAG = "JZVD";
    public static int SYSTEM_UI = 0;

    public static String stringForTime(long timeMs) {
        if (timeMs <= 0 || timeMs >= 24 * 60 * 60 * 1000) {
            return "00:00";
        }
        long totalSeconds = timeMs / 1000;
        int seconds = (int) (totalSeconds % 60);
        int minutes = (int) ((totalSeconds / 60) % 60);
        int hours = (int) (totalSeconds / 3600);
        StringBuilder stringBuilder = new StringBuilder();
        Formatter mFormatter = new Formatter(stringBuilder, Locale.getDefault());
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }


    /**
     * 是否是WiFi连接
     *
     * @param context 上下文
     * @return true表示WiFi连接，并且可以上网。false表示WiFi没连接，或者WiFi连接了，但是不能上网
     */
    public static boolean isWifiConnected(Context context) {
        // 获取网络管理对象
        NetManager netManager = NetManager.getInstance(context);
        // 获取NetCapabilities对象
        NetCapabilities netCapabilities = netManager.getNetCapabilities(netManager.getDefaultNet());
        // NetCapabilities.NET_CAPABILITY_VALIDATED表示连接了网络，并且可以上网
        return netCapabilities.hasCap(NetCapabilities.NET_CAPABILITY_VALIDATED) &&
                netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI) ||
                netCapabilities.hasBearer(NetCapabilities.BEARER_WIFI_AWARE);
    }

    /**
     * Get activity from context object
     *
     * @param context context
     * @return object of Activity or null if it is not Activity
     */
    public static Ability scanForActivity(Context context) {
        if (context == null) return null;

        if (context instanceof Ability) {
            return (Ability) context;
        } else if (context instanceof AbilityContext) {
            return scanForActivity(((AbilityContext) context).getContext());
        }

        return null;
    }

    public static void setRequestedOrientation(Context context, AbilityInfo.DisplayOrientation orientation) {
        if (JZUtils.scanForActivity(context) != null) {
            JZUtils.scanForActivity(context).setDisplayOrientation(orientation);
        } else {
            JZUtils.scanForActivity(context).setDisplayOrientation(orientation);
        }
    }

    public static Window getWindow(Context context) {
        if (JZUtils.scanForActivity(context) != null) {
            return JZUtils.scanForActivity(context).getWindow();
        } else {
            return JZUtils.scanForActivity(context).getWindow();
        }
    }

    public static ComponentContainer getDockerView(Component component){
        ComponentParent componentParent= component.getComponentParent();
        while (componentParent.getComponentParent()!=null){
            componentParent=componentParent.getComponentParent();
        }

        return (ComponentContainer)componentParent;
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = AttrHelper.vp2px(dpValue, context);
        return (int) (dpValue * scale + 0.5f);
    }

    public static void saveProgress(Context context, Object url, long progress) {
        if (!Jzvd.SAVE_PROGRESS) return;
        Log.i(TAG, "saveProgress: " + progress);
        if (progress < 5000) {
            progress = 0;
        }

        DatabaseHelper databaseHelper = new DatabaseHelper(context); // context入参类型为ohos.app.Context。
        Preferences editor = databaseHelper.getPreferences("JZVD_PROGRESS");
        editor.putLong("newVersion:" + url.toString(), progress).flush();
    }

    public static long getSavedProgress(Context context, Object url) {
        if (!Jzvd.SAVE_PROGRESS) return 0;
        DatabaseHelper databaseHelper = new DatabaseHelper(context); // context入参类型为ohos.app.Context。
        Preferences editor = databaseHelper.getPreferences("JZVD_PROGRESS");
        return editor.getLong("newVersion:" + url.toString(), 0);
    }

    /**
     * if url == null, clear all progress
     *
     * @param context context
     * @param url     if url!=null clear this url progress
     */
    public static void clearSavedProgress(Context context, Object url) {
        DatabaseHelper databaseHelper = new DatabaseHelper(context); // context入参类型为ohos.app.Context。
        Preferences editor = databaseHelper.getPreferences("JZVD_PROGRESS");

        if (url == null) {
            editor.clear().flush();
        } else {
            editor.putLong("newVersion:" + url.toString(), 0).flush();
        }
    }

    public static void showStatusBar(Context context) {
        if (Jzvd.TOOL_BAR_EXIST) {
            JZUtils.getWindow(context).clearFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        }
    }

    //如果是沉浸式的，全屏前就没有状态栏
    public static void hideStatusBar(Context context) {
        if (Jzvd.TOOL_BAR_EXIST) {
            JZUtils.getWindow(context).addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        }
    }

    public static void hideSystemUI(Context context) {
        WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.INVISIBLE);
    }

    public static void showSystemUI(Context context) {
        WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.VISIBLE);
    }

    //获取状态栏的高度
    public static int getStatusBarHeight(Context context) {

        return 0;
    }

    //获取NavigationBar的高度
    public static int getNavigationBarHeight(Context context) {
       return 0;
    }

    //获取屏幕的宽度
    public static int getScreenWidth(Context context) {
        return AttrHelper.vp2px(context.getResourceManager().getDeviceCapability().width, context);
    }

    //获取屏幕的高度
    public static int getScreenHeight(Context context) {
        return AttrHelper.vp2px(context.getResourceManager().getDeviceCapability().height, context);
    }

}
